#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

using std::cerr;
using std::endl;
using std::cout;


int main(int argc, char *argv[]){
    const char *dir_path;
    
    // 打开目录流
    if(argc == 1){
        dir_path = "../corpus/CN/";
    }else if(argc == 2){
        dir_path = argv[1];
    }

    DIR *dir = opendir(dir_path);
    if(dir == nullptr){
        cerr << "open dir failed, check if your dir path is vaild." << endl;
        exit(1);
    }

    struct dirent *dir_info;
    
    while((dir_info = readdir(dir)) != nullptr){
        // cout << "d_ino:" << dir_info->d_ino << endl;
        // cout << "d_off:" << dir_info->d_off << endl;
        // cout << "d_reclen:" << dir_info->d_reclen << endl;
        // cout << "d_type:" << dir_info->d_type << endl;
        // cout << "d_name:" << dir_info->d_name << endl;

        const char *file_name = dir_info->d_name;
        // =过滤掉 '.' 和 '..'
        if (strcmp(file_name, ".") == 0 || strcmp(file_name, "..") == 0){
            continue;
        }

        if (dir_info->d_type == DT_REG){
            // 判断文件类型是否为文件
            cout << file_name << endl;
        }else{
            continue;
        }
    }

    // 关闭目录流
    closedir(dir);

    return 0;
}

